﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDSP.BRL;
using BLL.Util;
using System.IO;

namespace LDSP.Util.Mail
{
    public class NewsMailInfo : MailInfo
    {
        private News _news;
        public News CurrentNews
        {
            get
            {
                _news = new News();

                int id = 0;
                if (Int32.TryParse(SelectedIds, out id))
                {
                    _news.GetProperties(id);
                }

                return _news;
            }
        }

        public NewsMailInfo(string selectedIds)
            : base(selectedIds)
        {
            Subject = "EHS Newsletter";
        }

        public override void InitMailContent()
        {
            string attachList = string.Empty;
            if (CurrentNews != null && !string.IsNullOrEmpty(CurrentNews.Name))
            {
                //若有附件，则将法规名作为邮件发送的附件名。
                if (!string.IsNullOrEmpty(CurrentNews.FileName))
                {
                    attachList += CurrentNews.Name + ".pdf;";
                }
            }

            Subject = "EHS Newsletter " + CurrentNews.Name;
            Attachments = attachList;
            Content =
@"Dear all valued client,

EHS Newsletter " + CurrentNews.Name + @"


Any questions/ suggetsion to newsletter and services, pls contact me freely.

 
Pablo
Associate
Shanghai China
Dow & Ann Consulting Shanghai Ltd
Tel: 021- 5109 8065
Fax: 021-5104 0716
MP: 0136 6193 0439
";
        }

        public override IList<string> GetAttachmentList(System.Web.HttpServerUtility server)
        {
            IList<string> fileList = new List<string>();

            //拷贝文件到临时目录。
            string source = server.MapPath("~\\Docs\\" + CurrentNews.FileExtension);

            if (File.Exists(source))
            {//若有法规附件，则添加到邮件附件。
                string destination = server.MapPath("~\\TmpImg\\" + CurrentNews.Name + ".pdf");

                if (!File.Exists(destination))
                {
                    File.Copy(source, destination);
                }

                fileList.Add(destination);
            }
            else if (!string.IsNullOrEmpty(CurrentNews.FileName))
            {//若无附件，但系统数据里有文件名，则添加空字串进附件文件列表。
                fileList.Add("");
            }

            return fileList;
        }
    }
}
